import os
import re
import time
from time import sleep
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException
from base_way import Base_Way, ReadYaml, GetPath


class LoginPage():
    # 修改表单的yaml文件
    yaml_path = GetPath().getpath(relative_path=r'\input.yaml')
    yaml_data = ReadYaml().readyaml(yaml_path)
    driver = Base_Way("C")
    success_mp3 = './yusu.mp3'
    url = yaml_data.get("login").get("url")
    user_name = yaml_data.get("login").get("user_name")
    password = yaml_data.get("login").get("password")

    def execute_js_ele(self, re_str, num):
        data_d = self.driver.driver.page_source
        # ele_id = re.findall("el-autocomplete-[0-9]*-item-0", data_d)
        ele_id = re.findall(re_str, data_d)
        js = "ele = document.getElementById('{0}');ele.click()".format(
            f"{ele_id[num]}")
        self.driver.execute_script(js)

    def play_mp3(self, file_path):
        mp3_file = GetPath().getpath(relative_path="\\" + file_path)
        os.system(mp3_file)

    def get_re_data(self, re_str):
        data_d = self.driver.driver.page_source
        li_num = len(re.findall(re_str, data_d))
        return li_num

    def find_select_until_show(self, element, func_):
        while True:
            try:
                ele = self.driver.boolean_ele(element)
                if ele:
                    func_(element)
                    break
            except Exception as e:
                print(e)
                continue

    def click_my_price(self):
        sleep(6)
        i = 1
        while True:

            # //*[@id="pricing"]/section/div/div/h2
            sleep(10)

            sorry_txt_ele = 'x,//*[@id="pricing"]/section/div/div/h2'
            sorry_text = self.driver.boolean_ele(sorry_txt_ele)
            print(f'抱歉的文本出现：', sorry_text)

            user_ele = 'c,el-switch__label--right'
            #              //*[@id="pricing"]/div/div/div/div/div[1]/div[2]/ul/li[2]/ul/li[1]
            ship_ele = f'c,ico-shipment'
            route_ele = f'c,ico-service'
            shin_boolean = self.driver.boolean_eles(ship_ele)
            route_boolean = self.driver.boolean_eles(route_ele)
            user_boolean = self.driver.boolean_ele(user_ele)
            print(f"按钮存在：{user_boolean}   船名存在：{shin_boolean}   航线存在：{route_boolean}")
            if sorry_text or (not route_boolean and not user_boolean):

                print(f"第{i}次循环")
                # 点击修改搜索
                print("点击修改搜索")
                # self.driver.click('l,修改搜索')
                search_ele = 'l,修改搜索'
                self.find_select_until_show(search_ele, self.driver.click)
                # 滑动到到顶部
                print("滑动到到顶部")
                self.driver.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
                # 点击获取我的报价
                print("点击获取我的报价")
                self.driver.click('x,//*[@id="top-search-zone"]/section/form/div/div/button[1]/span/span')
                i = i + 1
                sleep(3)
            else:
                break

    def back_re_select(self):
        search_ele = 'l,修改搜索'
        self.find_select_until_show(search_ele, self.driver.click)
        # self.driver.click('l,修改搜索')
        js = "window.scrollTo(0,500)"
        self.driver.execute_script(js)
        self.driver.click('x,//*[@id="top-search-zone"]/section/form/div/div/button[1]/span/span')
        # //*[@id="SearchQuote"]
        sleep(5)

    def login(self):
        # 打开指定的url
        self.driver.open(self.url)

        element = 'x,//*[@id="navbarNavDropdown"]/ul/li[5]/a'
        self.find_select_until_show(element, self.driver.click)
        # self.driver.show_wait(180, 'x,//*[@id="body"]/header/nav[2]/div[3]/a')
        # # 输入用户名和密码，点击登录
        # self.driver.click('x,//*[@id="body"]/header/nav[2]/div[3]/a')
        sleep(3)
        self.driver.click('x,//*[@id="navbarNavDropdown"]/ul/li[5]/div/div/div/div[2]/div[1]/button[1]')
        self.driver.show_wait(5, 'i,login-email')
        self.driver.find_ele('i,login-email').clear()
        self.driver.input('i,login-email', self.user_name)
        self.driver.find_ele('i,login-password').clear()
        self.driver.input('i,login-password', self.password)
        self.driver.click('x,/html/body/div/div[2]/div[1]/form/fieldset/div[3]/button')
        sleep(4)
        print("登录成功")
        if self.driver.boolean_ele('c,wm-template-main-text'):
            print("有弹窗出现")
            self.driver.click('c,wm-close-button')
            print("点击关闭弹窗")

    def page_01(self):
        self.driver.show_wait(180, 'c,mycma')
        self.driver.click('c,mycma')
        sleep(1)
        self.driver.show_wait(180, 'x,//*[@id="navbarNavDropdown"]/ul/li[5]/div/div/div/div[1]/div[2]/ul/li[2]/a')
        self.driver.click('x,//*[@id="navbarNavDropdown"]/ul/li[5]/div/div/div/div[1]/div[2]/ul/li[2]/a')
        print("进入筛选页面")

    def page_02(self):
        self.driver.show_wait(20, 'x,//*[@id="top-search-zone"]/section/h1')
        # 起运港
        start_port = self.yaml_data.get("form").get("start_port")
        self.driver.input('x,//*[@id="top-search-zone"]/section/form/fieldset[1]/div/div[1]/div[2]/div/div/div[1]/input'
                          , start_port)
        sleep(2)
        self.execute_js_ele("el-autocomplete-[0-9]*-item-0", 0)
        sleep(2)
        to_port = self.yaml_data.get("form").get("to_port")
        # 卸货港
        self.driver.input('x,//*[@id="top-search-zone"]/section/form/fieldset[1]/div/div[1]/div[3]/div/div/div[1]/input'
                          , to_port)
        sleep(2)
        self.execute_js_ele("el-autocomplete-[0-9]*-item-0", 0)
        sleep(2)
        # 船舶离港日期
        leave_date = self.yaml_data.get("form").get("leave_date")
        self.driver.input('x,//*[@id="top-search-zone"]/section/form/fieldset[1]/div/div[2]/div/div/div/input',
                          leave_date)
        # 点击空白
        self.driver.click('x,//*[@id="top-search-zone"]/section/h1')
        js = "arguments[0].scrollIntoView();"
        # 滑动到指定元素位置
        # self.driver.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        # ele_get_my_price = self.driver.find_ele('i,SearchQuote')
        # self.driver.execute_script(js, ele_get_my_price)
        js = "window.scrollTo(0,500)"
        self.driver.execute_script(js)
        self.driver.click('x,//*[@id="top-search-zone"]/section/form/fieldset[2]/div[1]/div[1]/div/div/div/input')
        # //*[@id="DdlEquipmentType"]
        # 集装箱类型
        box_type = self.yaml_data.get("form").get("box_type")
        js = f'ele = document.getElementsByClassName("el-select-dropdown__item")[{box_type}];function func(){{console.log(12345)}};ele.onclick =func;ele.click()'
        self.driver.execute_script(js)
        # 按每只柜的重量（KGM）
        box_kg = self.yaml_data.get("form").get("box_kg")
        self.driver.input('x,//*[@id="top-search-zone"]/section/form/fieldset[2]/div[1]/div[2]/div/span/input',
                          box_kg)
        sleep(4)
        # 先删除后输入数量
        self.driver.key_event('x,//*[@id="top-search-zone"]/section/form/fieldset[2]/div[1]/div[3]/div/div/span/input',
                              "Keys.BACK_SPACE")
        self.driver.key_event('x,//*[@id="top-search-zone"]/section/form/fieldset[2]/div[1]/div[3]/div/div/span/input',
                              "Keys.BACK_SPACE")
        # 柜数
        cabintes_num = self.yaml_data.get("form").get("cabintes_num")
        self.driver.input('x,//*[@id="top-search-zone"]/section/form/fieldset[2]/div[1]/div[3]/div/div/span/input'
                          , cabintes_num)
        sleep(2)
        # 点击品名/海关编码
        print("点击海关编码")
        js1 = f'ele = document.getElementById("DdlCommodity");function func(){{console.log(12345)}};ele.onclick =func;ele.click()'
        self.driver.execute_script(js1)
        # self.driver.show_wait(180, 'x,//*[@id="DdlCommodity"]')
        # self.driver.click('x,//*[@id="DdlCommodity"]')
        # el-select-dropdown__item hover
        # 海关编码
        customs_code = self.yaml_data.get("form").get("customs_code")
        js2 = f'ele = document.getElementsByClassName("el-select-dropdown__item")[{customs_code}];function func(){{console.log(12345)}};ele.onclick =func;ele.click()'
        self.driver.execute_script(js2)

        self.driver.show_wait(180, ',//*[@id="top-search-zone"]/section/form/div/div/button[1]/span/span')

        # 点击获取我的报价
        self.driver.click('x,//*[@id="top-search-zone"]/section/form/div/div/button[1]/span/span')
        print("点击获取我的报价")
        while True:
            if self.driver.boolean_ele('x,//*[@id="pricing"]/div/div/header/div[1]/div/div/h1/span[1]'):
                break
            sleep(3)
            print("等待页面跳转")

        self.click_my_price()

    def page_03(self):
        break_flag = True
        while break_flag:
            # 获取船名和航线
            ship_name = self.yaml_data.get("list").get("ship_name")
            route_name = self.yaml_data.get("list").get("route_name")
            black_ship = self.yaml_data.get("list").get("black_ship")
            # 点击仅限可用解决方案
            button_ele = 'c,el-switch__core'
            button_ = len(self.driver.find_eles(button_ele))
            print("按钮个数：", button_)
            if button_ == 1:
                self.driver.click(button_ele)
            elif button_ == 2:
                self.driver.find_eles(button_ele)[1].click()
            # 滑动到底部
            print("滑动到底部")
            self.driver.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')


            data_d = self.driver.driver.page_source
            li_num = len(re.findall("departure-result-card", data_d))
            print("获取符合的列数：", li_num)
            result_ = self.driver.boolean_ele('c,showMore')
            print("显示更多结果：", result_)
            js_up1 = "window.scrollTo(500,0)"
            self.driver.execute_script(js_up1)
            self.driver.implicit_wait(20)
            # 循环点加载更多
            while result_:
                print(f"点击更多", )
                # 执行点击更多的脚本
                js1 = f'ele = document.getElementsByClassName("style-link showMore")[0];function func(){{console.log(12345)}};ele.onclick =func;ele.click()'
                self.driver.execute_script(js1)
                # 判断页面是否跳转到抱歉页面
                self.click_my_price()
                self.driver.implicit_wait(20)
                data_d = self.driver.driver.page_source
                li_num = len(re.findall("departure-result-card", data_d))
                print(li_num)
                # 滑动到底部
                print("滑动到底部")
                self.driver.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
                # 往上滑
                print("往上滑  500")
                js_down = "window.scrollTo(200,0)"
                self.driver.execute_script(js_down)
                # 判断点击更多按钮是否存在
                result_ = self.driver.boolean_ele('c,showMore')
            data_d = self.driver.driver.page_source
            li_num1 = len(re.findall("departure-result-card", data_d))
            print('最后可以点击的列数：', li_num1)
            print("船名：  " + str(ship_name))
            print("航线：  " + str(route_name))
            if len(black_ship) == 0:
                if len(ship_name) == 0 and len(route_name) == 0:
                    print("len(ship_name) == 0 and len(route_name) == 0")
                    js_se = f'ele = document.getElementsByClassName("o-button primary")[4];ele.click()'
                    self.driver.execute_script(js_se)
                    sleep(2)
                    user_text = '立刻使用此优惠订舱'
                    next_text = "下一步"
                    get_my_text = "获取我的报价"
                    # 下一步 元素
                    next_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                    get_my_price = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                    use_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button[1]/span'
                    immediately_use = 'x,//*[@id="pricing"]/div/div/footer/div/div/button[1]/span'
                    if self.driver.get_text(next_ele) == next_text:
                        print("点击下一步")
                        self.driver.click(next_ele)
                        sleep(2)
                        if self.driver.get_text(get_my_price) == get_my_text:
                            try:
                                print("点击获取我的报价")
                                self.driver.click(get_my_price)
                                sleep(7)
                                self.driver.click(immediately_use)
                                print("已找到你想要的航线，播放音频=================================")
                                success_mp3 = self.yaml_data.get("mp3_list").get("success_mp3")
                                self.play_mp3(success_mp3)
                            except NoSuchElementException:
                                print("系统错误")
                            break
                        else:
                            print("页面加载失败，请重新运行")
                            break
                    elif self.driver.get_text(use_ele) == user_text:
                        print("进入到错误的界面，播放哭声")
                        fail_mp3 = self.yaml_data.get("mp3_list").get("fail_mp3")
                        self.play_mp3(fail_mp3)
                        break

                elif len(ship_name) != 0 and len(route_name) == 0:
                    print("len(ship_name) != 0 and len(route_name) == 0")
                    for i in range(1, li_num1 + 1):
                        ship_txt = self.driver.find_eles('c,ico-shipment')[i - 1].text
                        print(f"第{i}列，船名：", ship_txt)
                        route_txt = str(self.driver.find_eles('c,ico-service')[i - 1].text).split(" ")[1]
                        print(f"第{i}列，航线名：", route_txt)
                        if ship_txt in ship_name:
                            print(f"{ship_name}符合============")
                            # 点击相应选择
                            # //*[@id="pricing"]/div/div/div/div[2]/div[1]
                            # //*[@id="pricing"]/div/div/div/div[2]/div[2]
                            js_se = f'ele = document.getElementsByClassName("o-button primary")[{i-1}];ele.click()'
                            self.driver.execute_script(js_se)
                            sleep(2)
                            user_text = '立刻使用此优惠订舱'
                            next_text = "下一步"
                            get_my_text = "获取我的报价"
                            # 下一步 元素
                            next_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            #
                            get_my_price = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            use_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button[1]/span'

                            immediately_use = 'x,//*[@id="pricing"]/div/div/footer/div/div/button[1]/span'

                            if self.driver.get_text(next_ele) == next_text:
                                print("点击下一步")
                                self.driver.click(next_ele)
                                sleep(2)
                                if self.driver.get_text(get_my_price) == get_my_text:
                                    try:
                                        print("点击获取我的报价")
                                        self.driver.click(get_my_price)
                                        sleep(7)
                                        self.driver.click(immediately_use)
                                        print("已找到你想要的航线，播放音频=================================")
                                        success_mp3 = self.yaml_data.get("mp3_list").get("success_mp3")
                                        self.play_mp3(success_mp3)
                                    except NoSuchElementException:
                                        print("系统错误")
                                    break_flag = False
                                    break
                                else:
                                    print("页面加载失败，请重新运行")
                                    break_flag = False
                                    break
                            elif self.driver.get_text(use_ele) == user_text:
                                print("进入到错误的界面，播放哭声")
                                fail_mp3 = self.yaml_data.get("mp3_list").get("fail_mp3")
                                self.play_mp3(fail_mp3)
                                break_flag = False
                                break
                    else:
                        self.back_re_select()
                        self.click_my_price()

                elif len(ship_name) == 0 and len(route_name) != 0:
                    print("len(ship_name) == 0 and len(route_name) != 0")
                    for i in range(1, li_num1 + 1):
                        ship_txt = self.driver.find_eles('c,ico-shipment')[i - 1].text
                        print(f"第{i}列，船名：", ship_txt)
                        route_txt = str(self.driver.find_eles('c,ico-service')[i - 1].text).split(" ")[1]
                        print(f"第{i}列，航线名：", route_txt)
                        if route_txt in route_name:
                            print(f"{route_name}符合============")
                            # 点击相应选择
                            # //*[@id="pricing"]/div/div/div/div[2]/div[1]
                            # //*[@id="pricing"]/div/div/div/div[2]/div[2]
                            js_se = f'ele = document.getElementsByClassName("o-button primary")[{i-1}];ele.click()'
                            self.driver.execute_script(js_se)
                            sleep(2)
                            user_text = '立刻使用此优惠订舱'
                            next_text = "下一步"
                            get_my_text = "获取我的报价"
                            # 下一步 元素
                            next_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            #
                            get_my_price = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            use_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button[1]/span'

                            immediately_use = 'x,//*[@id="pricing"]/div/div/footer/div/div/button[1]/span'

                            if self.driver.get_text(next_ele) == next_text:
                                print("点击下一步")
                                self.driver.click(next_ele)
                                sleep(2)
                                if self.driver.get_text(get_my_price) == get_my_text:
                                    try:
                                        print("点击获取我的报价")
                                        self.driver.click(get_my_price)
                                        sleep(7)
                                        self.driver.click(immediately_use)
                                        print("已找到你想要的航线，播放音频=================================")
                                        success_mp3 = self.yaml_data.get("mp3_list").get("success_mp3")
                                        self.play_mp3(success_mp3)
                                    except NoSuchElementException:
                                        print("系统错误")
                                    break_flag = False
                                    break
                                else:
                                    print("页面加载失败，请重新运行")
                                    break_flag = False
                                    break
                            elif self.driver.get_text(use_ele) == user_text:
                                print("进入到错误的界面，播放哭声")
                                fail_mp3 = self.yaml_data.get("mp3_list").get("fail_mp3")
                                self.play_mp3(fail_mp3)
                                break_flag = False
                                break
                    else:
                        self.back_re_select()
                        self.click_my_price()

                elif len(ship_name) != 0 and len(route_name) != 0:
                    print("len(ship_name) != 0 and len(route_name) != 0")
                    for i in range(1, li_num1 + 1):
                        ship_txt = self.driver.find_eles('c,ico-shipment')[i - 1].text
                        print(f"第{i}列，船名：", ship_txt)
                        route_txt = str(self.driver.find_eles('c,ico-service')[i - 1].text).split(" ")[1]
                        print(f"第{i}列，航线名：", route_txt)
                        if (ship_txt in ship_name) or (route_txt in route_name):
                            print("航线，船只符合其一============")
                            # 点击相应选择
                            # //*[@id="pricing"]/div/div/div/div[2]/div[1]
                            # //*[@id="pricing"]/div/div/div/div[2]/div[2]
                            js_se = f'ele = document.getElementsByClassName("o-button primary")[{i-1}];ele.click()'
                            self.driver.execute_script(js_se)
                            sleep(2)
                            user_text = '立刻使用此优惠订舱'
                            next_text = "下一步"
                            get_my_text = "获取我的报价"
                            # 下一步 元素
                            next_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            #
                            get_my_price = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            use_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button[1]/span'

                            immediately_use = 'x,//*[@id="pricing"]/div/div/footer/div/div/button[1]/span'

                            if self.driver.get_text(next_ele) == next_text:
                                print("点击下一步")
                                self.driver.click(next_ele)
                                sleep(2)
                                if self.driver.get_text(get_my_price) == get_my_text:
                                    try:
                                        print("点击获取我的报价")
                                        self.driver.click(get_my_price)
                                        sleep(7)
                                        self.driver.click(immediately_use)
                                        print("已找到你想要的航线，播放音频=================================")
                                        success_mp3 = self.yaml_data.get("mp3_list").get("success_mp3")
                                        self.play_mp3(success_mp3)
                                    except NoSuchElementException:
                                        print("系统错误")
                                    break_flag = False
                                    break
                                else:
                                    print("页面加载失败，请重新运行")
                                    break_flag = False
                                    break
                            elif self.driver.get_text(use_ele) == user_text:
                                print("进入到错误的界面，播放哭声")
                                fail_mp3 = self.yaml_data.get("mp3_list").get("fail_mp3")
                                self.play_mp3(fail_mp3)
                                break_flag = False
                                break
                    else:
                        self.back_re_select()
                        self.click_my_price()

            elif len(black_ship) != 0:
                print("有黑名单:", black_ship)
                if len(ship_name) == 0 and len(route_name) == 0:
                    print("len(ship_name) == 0 and len(route_name) == 0")
                    for i in range(1, li_num1 + 1):
                        # 船名元素
                        ship_txt = self.driver.find_eles('c,ico-shipment')[i - 1].text
                        print(f"第{i}列，船名：", ship_txt)
                        route_txt = str(self.driver.find_eles('c,ico-service')[i - 1].text).split(" ")[1]
                        print(f"第{i}列，航线名：", route_txt)
                        if ship_txt in black_ship:
                            print(f"第{i}列的船名：{ship_txt}在黑名单船只里面=====：{black_ship}")
                            continue
                        else:
                            js_se = f'ele = document.getElementsByClassName("o-button primary")[{i-1}];ele.click()'
                            self.driver.execute_script(js_se)
                            sleep(2)
                            user_text = '立刻使用此优惠订舱'
                            next_text = "下一步"
                            get_my_text = "获取我的报价"
                            # 下一步 元素
                            next_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            #
                            get_my_price = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            use_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button[1]/span'

                            immediately_use = 'x,//*[@id="pricing"]/div/div/footer/div/div/button[1]/span'

                            if self.driver.get_text(next_ele) == next_text:
                                print("点击下一步")
                                self.driver.click(next_ele)
                                sleep(2)
                                if self.driver.get_text(get_my_price) == get_my_text:
                                    try:
                                        print("点击获取我的报价")
                                        self.driver.click(get_my_price)
                                        sleep(7)
                                        self.driver.click(immediately_use)
                                        print("已找到你想要的航线，播放音频=================================")
                                        success_mp3 = self.yaml_data.get("mp3_list").get("success_mp3")
                                        self.play_mp3(success_mp3)
                                    except NoSuchElementException:
                                        print("系统错误")
                                    break_flag = False
                                    break
                                else:
                                    print("页面加载失败，请重新运行")
                                    break_flag = False
                                    break
                            elif self.driver.get_text(use_ele) == user_text:
                                print("进入到错误的界面，播放哭声")
                                fail_mp3 = self.yaml_data.get("mp3_list").get("fail_mp3")
                                self.play_mp3(fail_mp3)
                                break_flag = False
                                break
                    else:
                        self.back_re_select()
                        self.click_my_price()

                elif len(ship_name) != 0 and len(route_name) == 0:
                    print("len(ship_name) != 0 and len(route_name) == 0")
                    for i in range(1, li_num1 + 1):
                        ship_txt = self.driver.find_eles('c,ico-shipment')[i - 1].text
                        print(f"第{i}列，船名：", ship_txt)
                        route_txt = str(self.driver.find_eles('c,ico-service')[i - 1].text).split(" ")[1]
                        print(f"第{i}列，航线名：", route_txt)
                        if ship_txt in black_ship:
                            print(f"第{i}列的船名：{ship_txt}在黑名单船只里面=====：{black_ship}")
                            continue

                        if ship_txt in ship_name:
                            print(f"{ship_txt}符合============")
                            # 点击相应选择
                            # //*[@id="pricing"]/div/div/div/div[2]/div[1]
                            # //*[@id="pricing"]/div/div/div/div[2]/div[2]
                            js_se = f'ele = document.getElementsByClassName("o-button primary")[{i-1}];ele.click()'
                            self.driver.execute_script(js_se)
                            sleep(2)
                            user_text = '立刻使用此优惠订舱'
                            next_text = "下一步"
                            get_my_text = "获取我的报价"
                            # 下一步 元素
                            next_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            #
                            get_my_price = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            use_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button[1]/span'

                            immediately_use = 'x,//*[@id="pricing"]/div/div/footer/div/div/button[1]/span'

                            if self.driver.get_text(next_ele) == next_text:
                                print("点击下一步")
                                self.driver.click(next_ele)
                                sleep(2)
                                if self.driver.get_text(get_my_price) == get_my_text:
                                    try:
                                        print("点击获取我的报价")
                                        self.driver.click(get_my_price)
                                        sleep(7)
                                        self.driver.click(immediately_use)
                                        print("已找到你想要的航线，播放音频=================================")
                                        success_mp3 = self.yaml_data.get("mp3_list").get("success_mp3")
                                        self.play_mp3(success_mp3)
                                    except NoSuchElementException:
                                        print("系统错误")
                                    break_flag = False
                                    break
                                else:
                                    print("页面加载失败，请重新运行")
                                    break_flag = False
                                    break
                            elif self.driver.get_text(use_ele) == user_text:
                                print("进入到错误的界面，播放哭声")
                                fail_mp3 = self.yaml_data.get("mp3_list").get("fail_mp3")
                                self.play_mp3(fail_mp3)
                                break_flag = False
                                break
                    else:
                        self.back_re_select()
                        self.click_my_price()

                elif len(ship_name) == 0 and len(route_name) != 0:
                    print("len(ship_name) == 0 and len(route_name) != 0 ")
                    for i in range(1, li_num1 + 1):
                        ship_txt = self.driver.find_eles('c,ico-shipment')[i - 1].text
                        print(f"第{i}列，船名：", ship_txt)
                        route_txt = str(self.driver.find_eles('c,ico-service')[i - 1].text).split(" ")[1]
                        print(f"第{i}列，航线名：", route_txt)
                        if ship_txt in black_ship:
                            print(f"第{i}列的船名：{ship_txt}在黑名单船只里面=====：{black_ship}")
                            continue

                        if route_txt in route_name:
                            print("航线，船只符合其一============")
                            # 点击相应选择
                            # //*[@id="pricing"]/div/div/div/div[2]/div[1]
                            # //*[@id="pricing"]/div/div/div/div[2]/div[2]
                            js_se = f'ele = document.getElementsByClassName("o-button primary")[{i-1}];ele.click()'
                            self.driver.execute_script(js_se)
                            sleep(2)
                            user_text = '立刻使用此优惠订舱'
                            next_text = "下一步"
                            get_my_text = "获取我的报价"
                            # 下一步 元素
                            next_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            #
                            get_my_price = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            use_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button[1]/span'

                            immediately_use = 'x,//*[@id="pricing"]/div/div/footer/div/div/button[1]/span'

                            if self.driver.get_text(next_ele) == next_text:
                                print("点击下一步")
                                self.driver.click(next_ele)
                                sleep(2)
                                if self.driver.get_text(get_my_price) == get_my_text:
                                    try:
                                        print("点击获取我的报价")
                                        self.driver.click(get_my_price)
                                        sleep(7)
                                        self.driver.click(immediately_use)
                                        print("已找到你想要的航线，播放音频=================================")
                                        success_mp3 = self.yaml_data.get("mp3_list").get("success_mp3")
                                        self.play_mp3(success_mp3)
                                    except NoSuchElementException:
                                        print("系统错误")
                                    break_flag = False
                                    break
                                else:
                                    print("页面加载失败，请重新运行")
                                    break_flag = False
                                    break
                            elif self.driver.get_text(use_ele) == user_text:
                                print("进入到错误的界面，播放哭声")
                                fail_mp3 = self.yaml_data.get("mp3_list").get("fail_mp3")
                                self.play_mp3(fail_mp3)
                                break_flag = False
                                break
                    else:
                        self.back_re_select()
                        self.click_my_price()

                elif len(ship_name) != 0 and len(route_name) != 0:
                    print("len(ship_name) != 0 and len(route_name) != 0")
                    for i in range(1, li_num1 + 1):
                        # 船名元素
                        # 航线
                        # 选择按钮
                        ship_txt = self.driver.find_eles('c,ico-shipment')[i - 1].text
                        print(f"第{i}列，船名：", ship_txt)
                        route_txt = str(self.driver.find_eles('c,ico-service')[i - 1].text).split(" ")[1]
                        print(f"第{i}列，航线名：", route_txt)
                        if ship_txt in black_ship:
                            print(f"第{i}列的船名：{ship_txt}在黑名单船只里面=====：{black_ship}")
                            continue
                        if (ship_txt in ship_name) or (route_txt in route_name):
                            print("航线，船只符合其一============")
                            # 点击相应选择
                            # //*[@id="pricing"]/div/div/div/div[2]/div[1]
                            # //*[@id="pricing"]/div/div/div/div[2]/div[2]
                            js_se = f'ele = document.getElementsByClassName("o-button primary")[{i-1}];ele.click()'
                            self.driver.execute_script(js_se)
                            sleep(2)
                            user_text = '立刻使用此优惠订舱'
                            next_text = "下一步"
                            get_my_text = "获取我的报价"
                            # 下一步 元素
                            next_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            #
                            get_my_price = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button/span'
                            use_ele = 'x,//*[@id="bestOfferDrawer"]/div/div/section/footer/div/div/button[1]/span'

                            immediately_use = 'x,//*[@id="pricing"]/div/div/footer/div/div/button[1]/span'

                            if self.driver.get_text(next_ele) == next_text:
                                print("点击下一步")
                                self.driver.click(next_ele)
                                sleep(2)
                                if self.driver.get_text(get_my_price) == get_my_text:
                                    try:
                                        print("点击获取我的报价")
                                        self.driver.click(get_my_price)
                                        sleep(7)
                                        self.driver.click(immediately_use)
                                        print("已找到你想要的航线，播放音频=================================")
                                        success_mp3 = self.yaml_data.get("mp3_list").get("success_mp3")
                                        self.play_mp3(success_mp3)
                                    except NoSuchElementException:
                                        print("系统错误")
                                    break_flag = False
                                    break
                                else:
                                    print("页面加载失败，请重新运行")
                                    break_flag = False
                                    break
                            elif self.driver.get_text(use_ele) == user_text:
                                print("进入到错误的界面，播放哭声")
                                fail_mp3 = self.yaml_data.get("mp3_list").get("fail_mp3")
                                self.play_mp3(fail_mp3)
                                break_flag = False
                                break
                    else:
                        self.back_re_select()
                        self.click_my_price()

    def page_04(self):
        # 等待回车
        flag_input = True
        while flag_input:
            yaml_path = GetPath().getpath(relative_path=r'\input.yaml')
            yaml_data = ReadYaml().readyaml(yaml_path)
            out_text = yaml_data.get("out")
            if bool(out_text):
                flag_input = False
            else:
                print(time.time())
                sleep(3)
                self.driver.window_handles_new()
                print("向下滚动")
                sleep(1)
                js_down = "window.scrollTo(0,500)"
                self.driver.execute_script(js_down)
                print("向上滚动")
                js_up = "window.scrollTo(500,0)"
                self.driver.execute_script(js_down)

    def debug(self):

        self.driver.open("http://www.baidu.com")
        sleep(1)
        print(self.driver.get_window())
        self.driver.click('l,图片')
        self.driver.window_handles_new()
        print(self.driver.get_window())




        # self.driver.switch_window(curr_window[-1])


if __name__ == '__main__':
    lg = LoginPage()
    lg.login()
    lg.page_01()
    lg.page_02()
    lg.page_03()
    lg.page_04()
    # lg.play_mp3('yesu.mp3')
    # lg.debug()
